home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / makestartupaliastome / debugf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  6.0 KB  |  180 lines

  1. /*
  2.     File: debugf.c
  3.     
  4.     Description:
  5.         debugf is like printf.  Routines implemented herein provide a subset
  6.     of the standard C library's sprintf functionality.  The differences
  7.     with this implementation include:
  8.     
  9.     it's small (around 2k once compiled)
  10.     
  11.     it doesn't call any toolbox or os routines.  it's entirely self contained
  12.     and does not call any toolbox routines or stdclib routines.  The only call
  13.     made outside of this library is the DebugStr call inside of the debugf
  14.     routine.
  15.  
  16.     it's safe to call at interrupt time (assuming there's the stack
  17.     space to do it).
  18.     
  19.     debugf can be called anywhere it is safe to call DebugStr.
  20.  
  21.     Copyright:
  22.         © Copyright 2000 Apple Computer, Inc. All rights reserved.
  23.     
  24.     Disclaimer:
  25.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  26.         ("Apple") in consideration of your agreement to the following terms, and your
  27.         use, installation, modification or redistribution of this Apple software
  28.         constitutes acceptance of these terms.  If you do not agree with these terms,
  29.         please do not use, install, modify or redistribute this Apple software.
  30.  
  31.         In consideration of your agreement to abide by the following terms, and subject
  32.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  33.         copyrights in this original Apple software (the "Apple Software"), to use,
  34.         reproduce, modify and redistribute the Apple Software, with or without
  35.         modifications, in source and/or binary forms; provided that if you redistribute
  36.         the Apple Software in its entirety and without modifications, you must retain
  37.         this notice and the following text and disclaimers in all such redistributions of
  38.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  39.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  40.         Apple Software without specific prior written permission from Apple.  Except as
  41.         expressly stated in this notice, no other rights or licenses, express or implied,
  42.         are granted by Apple herein, including but not limited to any patent rights that
  43.         may be infringed by your derivative works or by other works in which the Apple
  44.         Software may be incorporated.
  45.  
  46.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  47.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  48.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  49.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  50.         COMBINATION WITH YOUR PRODUCTS.
  51.  
  52.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  53.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  54.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  56.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  57.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  58.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59.  
  60.     Change History (most recent first):
  61.         Wed, Feb 16, 2000 -- created
  62. */
  63.  
  64. #include "debugf.h"
  65. #include <Types.h>
  66. #include <StdArg.h>
  67.  
  68. /* gotchas:
  69.     doesn't check for string buffer overflow.  There's an extra
  70.     768 bytes in string buffer to help avoid overflows,
  71.     but, 'use your head'
  72. */
  73.  
  74. int vsoutputf(char *s, const char *fmt, va_list args) {
  75.     char const *pull;
  76.     char *push, decb[32], *td;
  77.     unsigned char *rp;
  78.     const char *syms = "0123456789ABCDEF";
  79.     unsigned long value, i;
  80.     long base, digits;
  81.     Boolean twoscomp, hasDigits;
  82.     push = s;
  83.     pull = fmt;
  84.     while (*pull) {
  85.         if (*pull != '%') {
  86.             *push++ = *pull++;
  87.         } else {
  88.             pull++; /* skip the % */
  89.             if (*pull == '.') {
  90.                 pull++; /* skip the . */
  91.                 digits = 0;
  92.                 while (*pull >= '0' && *pull <= '9')
  93.                     digits = (digits * 10) + ((*pull++) - '0');
  94.                 hasDigits = true;
  95.             } else {
  96.                 digits = 0;
  97.                 hasDigits = false;
  98.             }
  99.             base = 0;
  100.             switch(*pull) {
  101.                 case 's':
  102.                     pull++;  /* skip the s */
  103.                     td = va_arg(args, char*);
  104.                     while (*td) {
  105.                         *push++ = *td++;
  106.                         digits--;
  107.                         if (hasDigits && digits <= 0) break;
  108.                     }
  109.                     break;
  110.                 case 'p':
  111.                     pull++;  /* skip the p */
  112.                     rp = va_arg(args, unsigned char*);
  113.                     for (i=0; i<*rp; i++) {
  114.                         *push++ = (char) (rp[i+1]);
  115.                         digits--;
  116.                         if (hasDigits && digits <= 0) break;
  117.                     }
  118.                     break;
  119.                 case 'c':  /* skip the c */
  120.                     pull++;
  121.                     *push++ =  (char) (va_arg(args, long));
  122.                     digits--;
  123.                     break;
  124.                 case 'b': pull++; base = 2; twoscomp = false; break;
  125.                 case 'n': pull++; base = 4; twoscomp = false; break;
  126.                 case 'o': pull++; base = 8; twoscomp = false; break;
  127.                 case 'd': pull++; base = 10; twoscomp = true; break;
  128.                 case 'u': pull++; base = 10; twoscomp = false; break;
  129.                 case 'x': pull++; base = 16; twoscomp = false; break;
  130.                 default: digits = 0; base = 0; break;
  131.             }
  132.             if (base == 0) {
  133.                 while (digits > 0) { *push++ = ' '; digits--; }
  134.             } else {
  135.                 value = va_arg(args, unsigned long);
  136.                 if (twoscomp && (value&0x80000000) != 0) {
  137.                     *push++ = '-'; value = ((~value)+1);
  138.                 }
  139.                 td = decb;
  140.                 do {    *td++ = syms[value%base];
  141.                     digits--;
  142.                     value /= base;
  143.                 } while (value != 0);
  144.                 while (digits > 0) { *push++ = '0'; digits--; }
  145.                 while (td != decb) *push++ = *--td;
  146.             }
  147.         }
  148.     }
  149.     return push - s;
  150. }
  151.  
  152.  
  153. StringPtr PLoutputf(StringPtr p, const char *fmt, ...) {
  154.     va_list args;
  155.     va_start(args, fmt);
  156.     p[0] = vsoutputf((char*) (p+1), fmt, args);
  157.     va_end(args);
  158.     return p;
  159. }
  160.  
  161. int soutputf(char *s, const char *fmt, ...) {
  162.     int result;
  163.     va_list args;
  164.     va_start(args, fmt);
  165.     result = vsoutputf(s, fmt, args);
  166.     s[result] = '\0';
  167.     va_end(args);
  168.     return result;
  169. }
  170.  
  171.  
  172. void debugf(char const *fmt, ...) {
  173.     char s[512];
  174.     va_list args;
  175.     va_start(args, fmt);
  176.     s[0] = vsoutputf(s+1, fmt, args);
  177.     DebugStr((StringPtr) s);
  178.     va_end(args);
  179. }
  180.